package com.bangdream.michelia.view.activity.my;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.view.activity.base.SwipeBackActivity;
import com.bangdream.michelia.view.fragment.main.my.MyExamFragment;
import com.bangdream.michelia.view.fragment.main.my.MyMessageFragment;

/**
 * @date on 2018/8/11 11:43
 * @email 909732184@qq.com
 * 我的消息
 */
public class MyMessage extends SwipeBackActivity implements ViewPager.OnPageChangeListener{
    private ViewPager viewPager;
    private TextView[] btnTvs = new TextView[2];
    private FragmentPagerAdapter fragmentPagerAdapter;
    //选项卡被选中和未选中时的字体颜色
    private int onTextColor,offTextColor;
    //viewpage的线条指示器
    private RelativeLayout indicatorView;
    //记录移动的开始位置 初始0
    private int previousPosition = 0;
    //屏幕的宽度
    private int screenWidth;
    @Override
    public int getLayout() {
        return R.layout.activity_my_message;
    }

    @Override
    public void findId() {
        initTitleBar(" ","我的消息","",this);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenWidth = dm.widthPixels;//宽度
        viewPager = findViewById(R.id.viewPager);
        onTextColor = ContextCompat.getColor(this,R.color.common_red);
        offTextColor = ContextCompat.getColor(this,R.color.black_text);
        indicatorView = findViewById(R.id.indicatorView);
        btnTvs[0] = findViewById(R.id.btnTv1);
        btnTvs[1] = findViewById(R.id.btnTv2);
//        btnTvs[2] = findViewById(R.id.btnTv3);
        setViewData();
    }

    @Override
    public void setListener() {
        viewPager.addOnPageChangeListener(this);
        for(TextView textView:btnTvs){
            textView.setOnClickListener(this);
        }
    }

    @Override
    public void logic() {

    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //线条指示器移动
        TranslateAnimation ani = new TranslateAnimation(
                previousPosition * screenWidth/2,
                position * screenWidth/2, 0, 0);
        ani.setDuration(200);
        ani.setFillAfter(true);
        ani.setInterpolator(new AccelerateDecelerateInterpolator());
        //播放动画
        indicatorView.startAnimation(ani);
        //将这一次的位置保存起来，作为下一次移动的起始位置
        previousPosition = position;

        for(int i = 0;i<btnTvs.length;i++){
            if(i==position){
                btnTvs[i].setTextColor(onTextColor);
            }
            else {
                btnTvs[i].setTextColor(offTextColor);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.btnTv1:
                viewPager.setCurrentItem(0);
                break;
            case R.id.btnTv2:
                viewPager.setCurrentItem(1);
                break;
//            case R.id.btnTv3:
//                viewPager.setCurrentItem(2);
//                break;
            default:break;
        }
    }

    //填充数据
    private void setViewData() {
        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                Bundle b = new Bundle();
                //因为整形默认为0  防止错误数据所以从1开始
                b.putInt("type",position+1);
                switch (position){
                    case 0:
                        MyMessageFragment myMessageFragment1 = new MyMessageFragment();
                        myMessageFragment1.setArguments(b);
                        return myMessageFragment1;
                    case 1:
                        MyMessageFragment myMessageFragment2 = new MyMessageFragment();
                        myMessageFragment2.setArguments(b);
                        return myMessageFragment2;

                    default:
                        return null;
                }
            }

            @Override
            public int getCount() {
                return btnTvs.length;
            }
        };
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.setOffscreenPageLimit(fragmentPagerAdapter.getCount());
    }
}
